package mapper;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.mybatis.mapper.OrdersMapperCustom;
import cn.itcast.mybatis.mapper.UserMapper;
import cn.itcast.mybatis.po.Orders;
import cn.itcast.mybatis.po.OrdersCustom;
import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserCustom;
import cn.itcast.mybatis.po.UserQueryVo;

/**
 *  使用classPath:mapper/usermapper.xml 为映射文件
 * @author E440
 *
 */
public class OrderCustomerDemo {
	private SqlSessionFactory factory=null;

	@Before
	public void beforeInstance() throws IOException{
		String config="config/sqlmapconfig.xml";
		InputStream in=Resources.getResourceAsStream(config);
		factory=new SqlSessionFactoryBuilder().build(in);
	}
	
	@Test
	public void testFindOrdersUserByResultMap() throws Exception {
		SqlSession session=factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		OrdersMapperCustom mapper=session.getMapper(OrdersMapperCustom.class);
		List<Orders> ordersCustom=mapper.findOrdersUserResultMap();
		System.out.println(ordersCustom);
		session.close();
	}
	
	@Test
	public void testFindOrdersUserByResultType() throws Exception {
		SqlSession session=factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		OrdersMapperCustom mapper=session.getMapper(OrdersMapperCustom.class);
		List<OrdersCustom> ordersCustom=mapper.findOrdersUser();
		System.out.println(ordersCustom);
		session.close();
	}
	
	@Test
	public void testFindOrdersAndOrderDetailResultMap() throws Exception {
		SqlSession session=factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		OrdersMapperCustom mapper=session.getMapper(OrdersMapperCustom.class);
		List<Orders> ordersCustom=mapper.findOrdersAndOrderDetailResultMap();
		System.out.println(ordersCustom);
		session.close();
	}
	
	@Test
	public void testFindUserAndItemsResultMap() throws Exception {
		SqlSession session=factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		OrdersMapperCustom mapper=session.getMapper(OrdersMapperCustom.class);
		List<User> ordersCustom=mapper.findUserAndItemsResultMap();
		System.out.println(ordersCustom);
		session.close();
	}
	
	// 查询订单关联查询用户，用户信息使用延迟加载
	@Test
	public void testFindOrdersUserLazyLoading() throws Exception {
		SqlSession sqlSession = factory.openSession();// 创建代理对象
		OrdersMapperCustom ordersMapperCustom = sqlSession
				.getMapper(OrdersMapperCustom.class);
		// 查询订单信息（单表）
		List<Orders> list = ordersMapperCustom.findOrdersUserLazyLoading();

		// 遍历上边的订单列表
		for (Orders orders : list) {
			// 执行getUser()去查询用户信息，这里实现按需加载
			User user = orders.getUser();
			System.out.println(user);
		}
	}
	
	// 一级缓存测试
	@Test
	public void testCache1() throws Exception {
		SqlSession sqlSession = factory.openSession();// 创建代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

		// 下边查询使用一个SqlSession
		// 第一次发起请求，查询id为1的用户
		User user1 = userMapper.findUserById(1);
		System.out.println(user1);

		// 如果sqlSession去执行commit操作（执行插入、更新、删除），清空SqlSession中的一级缓存，这样做的目的为了让缓存中存储的是最新的信息，避免脏读。

		// 更新user1的信息
		user1.setUsername("测试用户22");
		userMapper.updateUser(user1);
		//执行commit操作去清空缓存
		sqlSession.commit();

		// 第二次发起请求，查询id为1的用户
		User user2 = userMapper.findUserById(1);
		System.out.println(user2);

		sqlSession.close();
	}
	
	// 二级缓存测试
	@Test
	public void testCache2() throws Exception {
		SqlSession sqlSession1 = factory.openSession();
		SqlSession sqlSession2 = factory.openSession();
		SqlSession sqlSession3 = factory.openSession();
		// 创建代理对象
		UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
		// 第一次发起请求，查询id为1的用户
		User user1 = userMapper1.findUserById(1);
		System.out.println(user1);
		//这里执行关闭操作，将sqlsession中的数据写到二级缓存区域
		sqlSession1.close();
		
		//使用sqlSession3执行commit()操作
		UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
		User user  = userMapper3.findUserById(1);
		user.setUsername("张明明");
		userMapper3.updateUser(user);
		//执行提交，清空UserMapper下边的二级缓存
		sqlSession3.commit();
		sqlSession3.close();

		UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
		// 第二次发起请求，查询id为1的用户
		User user2 = userMapper2.findUserById(1);
		System.out.println(user2);

		sqlSession2.close();
	}
}
